package com.yf.springboot.task;

import com.yf.springboot.mapper.CronMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 基于注解设定多线程定时任务
 *
 * @author yvan
 */
// @Component
// @EnableScheduling   // 1.开启定时任务
// @EnableAsync        // 2.开启多线程
public class MultiThreadScheduleTask {
    //注入mapper
    @Resource
    private CronMapper cronMapper;

    @Async
    @Scheduled(fixedDelay = 3000)  // 间隔1秒
    public void first() throws InterruptedException {
        System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : "
            + Thread.currentThread().getName());
        System.out.println("--------------------------------\r\n");
        Thread.sleep(1000 * 10);
    }

    @Async
    @Scheduled(fixedDelay = 2000)
    public void second() {
        System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : "
            + Thread.currentThread().getName());
        System.out.println("cronMapper: " + cronMapper);
        System.out.println("--------------------------------\r\n");
    }
}